package coin.yang.thread;

public class SellTicketDemo {

    public static void main(String[] args) {

        Ticket t = new Ticket();
        t.setCount(100);
        // 开启四个线程，表示四个售票窗口
        Thread t1 = new Thread(new Seller(t));
        Thread t2 = new Thread(new Seller(t));
        Thread t3 = new Thread(new Seller(t));
        Thread t4 = new Thread(new Seller(t));
        // 启动
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }

}

class Seller implements Runnable {

    private Ticket t;

    public Seller(Ticket t) {
        this.t = t;
    }

    @Override
    public void run() {
        while (true) {
            // 同步代码块
            synchronized (Seller.class) {
                // 判断票的数量
                if (t.getCount() <= 0) break;
                // 卖出去一张票
                t.setCount(t.getCount() - 1);
                System.out.println("卖出去1张票，剩余" + t.getCount() + "张票");
            }
        }
    }
}

class Ticket {

    private int count;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}